import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'restaurant_provider.dart';

void main() {
  runApp(
    ChangeNotifierProvider(
      create: (context) => RestaurantProvider(),
      child: const MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  HomeScreenState createState() => HomeScreenState();
}

class HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    final restaurantProvider = Provider.of<RestaurantProvider>(context);
    final selectedRestaurant =
        restaurantProvider.restaurants.first; // 默认显示第一个餐厅
    return Scaffold(
      appBar: TDNavBar(
          height: 48,
          titleWidget: TDDropdownMenu(
            direction: TDDropdownMenuDirection.down,
            // labelBuilder: (context, label, isOpened, index) =>
            //     Text(_selectedText),
            onMenuOpened: (value) {
              print('打开第$value个菜单');
            },
            onMenuClosed: (value) {
              print('关闭第$value个菜单');
            },
            items: [
              TDDropdownItem(
                options: [
                  TDDropdownItemOption(
                      label: '高峰食堂', value: 'gf', selected: true),
                  TDDropdownItemOption(label: '采选食堂', value: 'cx'),
                  TDDropdownItemOption(label: '联发食堂', value: 'lf'),
                ],
                onChange: (value) {},
              ),
            ],
          ),
          titleFontWeight: FontWeight.w600,
          screenAdaptation: true,
          useDefaultBack: false,
          rightBarItems: [
            TDNavBarItem(
              icon: Icons.keyboard_outlined,
              iconSize: 30,
              action: () => {restaurantProvider.enterPhoneNumber(context)},
            ),
            TDNavBarItem(
              icon: TDIcons.scan,
              iconSize: 30,
              action: () => {restaurantProvider.scanQRCode(context)},
            ),
          ]),
      body: Column(
        children: [
          const SizedBox(height: 20),
          Text('已取份数: ${selectedRestaurant.takenMeals}'),
          Text('未取份数: ${selectedRestaurant.remainingMeals}'),
          Text('总份数: ${selectedRestaurant.totalMeals}'),
        ],
      ),
    );
  }
}
